﻿using Microsoft.EntityFrameworkCore;
using Naruto.Repository;
using Naruto.TaskManagement.Entitys;
using Naruto.TaskManagement.Entitys.Models;
using Naruto.TaskManagement.Entitys.VM;
using Naruto.TaskManagement.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Naruto.TaskManagement.Repository
{
    public class ChatSessionStateRepository : IChatSessionStateRepository
    {
        private readonly IRepository<TaskManagementContext> repository;

        public ChatSessionStateRepository(IRepository<TaskManagementContext> _repository)
        {
            repository = _repository;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="reciveUserId"></param>
        /// <returns></returns>
        public async Task<GetChatSessionStateViewModel> Get(string userId, string reciveUserId)
        {
            return default;
        }
        /// <summary>
        /// 查询id信息
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="reciveUserId"></param>
        /// <returns></returns>
        public async Task<string> GetId(string userId, string reciveUserId)
        {
            var query = await repository.QueryAsync<ChatSessionState>();
            return await query.Where(a => a.UserId == userId && a.ReciveUserId == reciveUserId).AsNoTracking()
                  .Select(a => a.Id).FirstOrDefaultAsync();
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task Add(ChatSessionState info)
        {
            await repository.Command<ChatSessionState>().AddAsync(info);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="chatSessionState"></param>
        /// <returns></returns>
        public async Task Update(ChatSessionState chatSessionState)
        {
            await repository.Command<ChatSessionState>().UpdateAsync(a => a.Id == chatSessionState.Id, entity =>
            {
                entity.UnReadMessageCount = chatSessionState.UnReadMessageCount;
                entity.ChatContent = chatSessionState.ChatContent;
                entity.ChatContentSendUserId = chatSessionState.ChatContentSendUserId;
                entity.ContentType = chatSessionState.ContentType;
                entity.UpdateTime = DateTime.Now;
                entity.IsDelete = false;
                return entity;
            });
        }

        /// <summary>
        /// 获取会话信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<List<GetListByUserIdViewModel>> GetListByUserId(string userId)
        {
            var query = await repository.QueryAsync<ChatSessionState>();
            return await query.Where(a => a.UserId == userId && a.IsDelete == false).OrderByDescending(a => a.UpdateTime).AsNoTracking()
                  .Select<ChatSessionState, GetListByUserIdViewModel>().ToListAsync();
        }

        /// <summary>
        /// 根据id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteById(string id)
        {
            await repository.Command<ChatSessionState>().UpdateAsync(a => a.Id == id, entity =>
             {
                 entity.IsDelete = true;
                 return entity;
             });
        }
    }
}
